package com.example.fixsys.controller;

import com.example.fixsys.common.QRCodeUtil;
import com.example.fixsys.common.Result;
import com.example.fixsys.common.SelfMathUtil;
import com.example.fixsys.entity.Users;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import java.awt.*;
import java.io.File;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@RestController
public class DetailController {
    @Resource
    private JdbcTemplate jdbcTemplate;

    @PostMapping("login")
    public Result<?> Login(@RequestBody Users users){
        String sql = "select userAccount,userPassword from users where userAccount=?";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql,new Object[]{users.getUserAccount()});
        SelfMathUtil selfMath = new SelfMathUtil();
        String psw = selfMath.setMaze(users.getUserPassword());
        Iterator<Map<String, Object>> it = list.iterator();
        if (it.hasNext()){
            Map<String, Object> map = it.next();
            return Result.success(map.containsValue(psw));
        }else return Result.error("12138","用户不存在");
    }

    @PostMapping("register")
    public Result<?> Register(@RequestBody Users users){
        int l = users.getUserPassword().length();
        if(l < 6 || l > 12)return Result.error("12138","密码长度不符合规范，请输入6~12位密码");
        String sql = "select userAccount from users";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        SelfMathUtil selfMath = new SelfMathUtil();
        String account = users.getUserAccount();
        String psw = selfMath.setMaze(users.getUserPassword());
        Iterator<Map<String, Object>> it = list.iterator();
        while (it.hasNext()){
            Map<String, Object> map = it.next();
            if(map.containsValue(account))return Result.error("12138","该用户已存在");
        }
        String str="insert into users(userAccount,userPassword) VALUES (?,?)";
        jdbcTemplate.update(str, new Object[]{account, psw});
        return Result.success();
    }

    @GetMapping("QRCodGenerate")
    public Result<?> QRCodGenerate(){
        Image image;
        QRCodeUtil qrCodeUtil = new QRCodeUtil();
        try {
            File file =new File("QRGenerationTest.jpg");
            ImageIO.write(qrCodeUtil.generateQrCode("QRGenerationTest","jpg"), "jpg", file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.success();
    }

    @GetMapping("repairCard")
    public Result<?> RepairCard(){
        String sql = "select recordNo,userName,deviceName,deviceAddress,reason,fixTime,fixStatus from record as r join device as d on r.deviceNo=d.deviceNo join users as u on r.fixApplicant=u.userNo";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        return Result.success(list);
    }

    @GetMapping("invoiceCard")
    public Result<?> InvoiceCard(){
        String sql = "select reimburseNo, userName, things, prize, saler, date, reimburseStatus from reimburse as r join users as u on r.fixWorker=u.userNo";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        return Result.success(list);
    }
}